import request from './request'
import type { ApiResponse } from './types'

// 模板数据类型
export interface TemplateData {
  id: number
  name: string
  type: string
  description: string
  kpis: { id: number; name: string; weight: number }[]
  createTime?: string
  updateTime?: string
}

// 模板分页查询参数
export interface TemplatePageParams {
  current: number
  size: number
  name?: string
  type?: string
}

// 模板分页响应数据
export interface TemplatePageResponse {
  records: TemplateData[]
  total: number
  size: number
  current: number
}

// 模板KPI关联数据
export interface TemplateKPI {
  templateId: number
  kpiId: number
  weight: number
}

// 获取模板列表
export const getTemplateList = () => {
  return request<ApiResponse<TemplateData[]>>({
    url: '/templates/list',
    method: 'post',
    data: {}
  })
}

// 获取模板分页列表
export const getTemplatePage = (params: TemplatePageParams) => {
  return request<ApiResponse<TemplatePageResponse>>({
    url: '/templates/page',
    method: 'post',
    data: params
  })
}

// 新增模板
export const addTemplate = (template: Omit<TemplateData, 'id' | 'createTime' | 'updateTime'>) => {
  return request<ApiResponse<number>>({
    url: '/templates/add',
    method: 'post',
    data: template
  })
}

// 编辑模板
export const updateTemplate = (template: TemplateData) => {
  return request<ApiResponse<boolean>>({
    url: '/templates/update',
    method: 'post',
    data: template
  })
}

// 删除模板
export const deleteTemplate = (id: number) => {
  return request<ApiResponse<boolean>>({
    url: '/templates/delete',
    method: 'post',
    data: { id }
  })
}

// 获取模板详情
export const getTemplateDetail = (id: number) => {
  return request<ApiResponse<TemplateData>>({
    url: '/templates/detail',
    method: 'post',
    data: { id }
  })
}

// 添加KPI到模板
export const addKPIToTemplate = (data: TemplateKPI) => {
  return request<ApiResponse<boolean>>({
    url: '/templates/add-kpi',
    method: 'post',
    data: data
  })
}

// 从模板移除KPI
export const removeKPIFromTemplate = (templateId: number, kpiId: number) => {
  return request<ApiResponse<boolean>>({
    url: '/templates/remove-kpi',
    method: 'post',
    data: { templateId, kpiId }
  })
}

// 更新模板KPI权重
export const updateTemplateKPIWeight = (data: TemplateKPI) => {
  return request<ApiResponse<boolean>>({
    url: '/templates/update-kpi-weight',
    method: 'post',
    data: data
  })
}